package cn.shengchao.order.user;

import cn.shengchao.base.event.PaySuccessMessageEvent;
import cn.shengchao.examstar.product.acl.ports.repositories.IProductCourseSettingRepository;
import cn.shengchao.examstar.rpc.orders.api.IOrderRpc;
import cn.shengchao.examstar.rpc.orders.res.PayOrderInfoDto;
import cn.shengchao.examstar.rpc.product.api.IProductRpc;
import cn.shengchao.examstar.rpc.product.base.ProductPropertyDto;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @program: examstarapi
 * @description: 支付成功后的产品处理
 * @author: Xiong weiqiang
 * @create: 2025-06-28 09:45
 */
@Component
public class PaySuccessOrderProductListener {
    @Autowired
    private IOrderRpc orderRpc;
    @Autowired
    private IProductRpc courseProductRpc;
    @Autowired
    UserOrderProductExpiredProcessor userOrderProductExpiredProcessor;
    @Autowired
    IProductCourseSettingRepository productCourseSettingRepository;
    @EventListener
    public void handleCustomEvent(PaySuccessMessageEvent event) {
        JSONObject order = JSON.parseObject(JSON.toJSONString(event.getSource()));
        //订单号
        String orderId = order.getString("tradeNo");
        PayOrderInfoDto payOrderInfoDto=orderRpc.getOrderInfoDto(Long.parseLong(orderId));
        //获取订单产品属性
        Map<Long,ProductPropertyDto> productPropertyDtoMap =productCourseSettingRepository.loadProductCourseSettingByOrderDto(payOrderInfoDto);
        userOrderProductExpiredProcessor.processUserProductAfterPay(productPropertyDtoMap,payOrderInfoDto);
    }
}